package com.yogurt.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yogurt.edu.entity.Chapter;
import com.yogurt.edu.entity.Video;
import com.yogurt.edu.entity.chapter.ChapterVideoVo;
import com.yogurt.edu.entity.chapter.VideoVo;
import com.yogurt.edu.mapper.ChapterMapper;
import com.yogurt.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yogurt.edu.service.VideoService;
import com.yogurt.servicebase.handler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author yogurt
 * @since 2020-07-10
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoService videoService;

    //根据课程id查询所有的章节和小节
    @Override
    public List<ChapterVideoVo> findChapterAndVideoByCourseId(String courseId) {
        //存放最终的结果集合
        List<ChapterVideoVo> result = new ArrayList<>();

        //根据课程id查询所有的章节
        QueryWrapper<Chapter> queryWrapperChapter = new QueryWrapper<>();
        //构建查询条件
        queryWrapperChapter.eq("course_id",courseId);
        //查询出所有的chapter对象,封装到list集合中
        List<Chapter> chapters = baseMapper.selectList(queryWrapperChapter);

        //根据课程id查询所有的小节
        QueryWrapper<Video> queryWrapperVideo = new QueryWrapper<>();
        //构建查询小节的条件,根据课程id查询所有的小节
        queryWrapperVideo.eq("course_id",courseId);
        //查询出所有的Video对象,封装到list集合中
        List<Video> videos = videoService.list(queryWrapperVideo);

        //创建ChapterVideoVo对象
        ChapterVideoVo chapterVideoVo = null;
        //循环遍历chapters，得到每一个chapter，将id和title值封装到chapterVideoVo中
        for (Chapter chapter : chapters) {
            chapterVideoVo = new ChapterVideoVo();
            /*
                chapterVideoVo.setId(chapter.getId());
                chapterVideoVo.setTitle(chapter.getTitle());
             */
            BeanUtils.copyProperties(chapter,chapterVideoVo);
            result.add(chapterVideoVo);

            //循环遍历videos,得到每一个video,将id和title放入videoVo中，再将其放入videoList集合中
            VideoVo videoVo = null;  //创建VideoVo对象
            //创建存放Video对象的集合
            List<VideoVo> videoList = new ArrayList<>();
            for (Video video : videos) {
                if (video.getChapterId().equals(chapter.getId())){
                    videoVo = new VideoVo();
                    BeanUtils.copyProperties(video,videoVo);
                    videoList.add(videoVo);
                }
            }
            chapterVideoVo.setChildren(videoList);
        }
        return result;
    }

    //根据章节id删除章节(如果章节中包含小节,则不能删除,反之,能删除)
    @Override
    public boolean deleteById(String chapterId) {
        //根据chapterId查询video(小节表),如果有数据,则不删除
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        //根据chapterId来查询有几条记录
        int count = videoService.count(wrapper);
        if (count > 0){
            //说明章节中包含小节,则不能删除
            throw new MyException(20001,"无法进行删除操作");
        }else {
            //章节中不包含小节,可以删除
            int result = baseMapper.deleteById(chapterId);
            //删除成功 result=1  失败  result=0
            return result > 0;
        }
    }

    //根据课程id删除章节
    @Override
    public void deleteChapterByCourseId(String courseId) {
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
